package com.uniknow.app.action;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.uniknow.app.bin.TBAppInfo;

/**
 * 应用信息提供类
 * 
 * @author zhenglibin
 * 
 */
public class ActionAppInfo {

//	private static final String tag = "AppInofProvider";
	private Context context;// 上下文
	private TBAppInfo appInfo;
	private List<TBAppInfo> appInfoList;

	public ActionAppInfo(Context context) {
		super();
		this.context = context;
	}

	/**
	 * 得到所有app信息
	 * @return
	 */
	@SuppressLint("NewApi")
	public List<TBAppInfo> getAppInfos() {

		PackageManager pm = context.getPackageManager();
		List<PackageInfo> packageInfos = pm
				.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
		appInfoList = new ArrayList<TBAppInfo>();
		for (PackageInfo packageInfo : packageInfos) {
			appInfo = new TBAppInfo();
			appInfo.setAppName(packageInfo.packageName);
			appInfo.setAppVersionName(packageInfo.versionName);
			appInfo.setAppVersionCode(packageInfo.versionCode);
			appInfo.setFirstInstallTime(packageInfo.firstInstallTime);
			appInfo.setLastUpdateTime(packageInfo.lastUpdateTime);
			appInfo.setIsUserApp(checkIsUserApp(packageInfo.applicationInfo));
			appInfo.setPackageName(packageInfo.packageName);
			appInfoList.add(appInfo);
			appInfo = null;
		}
		return appInfoList;
	}

	/**
	 * 检查是否为第三方应用
	 * @param appInfo
	 * @return
	 */
	private boolean checkIsUserApp(ApplicationInfo appInfo) {
		if (appInfo.flags == ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) {
			return true;
		} else {
			return false;
		}
	}
}
